package com.zpark.config;

import com.zpark.interceptor.LoginInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户相关拦截规则
 *      作用：当请求进来之后按照黑名单这的配置进行拦截，然后调用对应的拦截器执行类
 */
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {

    /**
     * 配置静态访问资源
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/zy/**").addResourceLocations("classpath:/zy/");
    }

    /**
     * 配置预先配置了响应状态代码和/或视图的简单自动化控制器，以呈现响应主体。
     * @param registry
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        // 当访问localhost:8080/的时候默认人跳转到login.html页面
        registry.addViewController("/").setViewName("/login.html");
        registry.addViewController("/index").setViewName("/login.html");
    }

    /**
     * 配置拦截规则的方法
     * @param registry 拦截注册表对象
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        // 创建拦截器对象
        HandlerInterceptor interceptor = new LoginInterceptor();
        // 注册拦截器
        InterceptorRegistration registration = registry.addInterceptor(interceptor);

        // 配置拦截路径黑名单
        List<String> blackList = new ArrayList<>();
        blackList.add("/**");// 拦截所有请求路径

        // 配置不拦截的路径
        List<String> whiteList = new ArrayList<>();
        whiteList.add("/login.html");
        whiteList.add("/user/login.do");

        // 添加拦截路径和不需要拦截的路径
        registration.addPathPatterns(blackList);
        registration.excludePathPatterns(whiteList);

    }
}
